package com.orchard.xpz.controller.gardenPlot;

import cn.afterturn.easypoi.csv.entity.CsvExportParams;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.orchard.xpz.entity.GardenPlot.GardenPlot;
import com.orchard.xpz.service.grandPlot.GardenPlotService;
import com.orchard.xpz.utils.PageResultData;
import com.orchard.xpz.utils.R;
import com.orchard.xpz.utils.UserRuntime;
import com.sun.deploy.net.HttpResponse;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * @TaskCode:
 * @description:
 * @author: minshuai.chen
 * @create: 2022/4/19 11:07
 **/
@RestController
@RequestMapping("/gardenplot")
public class GardenPlotController {

    @Autowired
    private GardenPlotService gardenPlotService;

    @RequestMapping("/getPageList")
    public R<PageResultData<GardenPlot>> getPageList(@RequestBody GardenPlot gardenPlot){
        return R.success(gardenPlotService.getPageList(gardenPlot));
    }

    @RequestMapping("/saveOrUpdate")
    public R<String> saveOrUpdate(@RequestBody GardenPlot gardenPlot){
        Thread t = Thread.currentThread();
        System.out.println(t+"==" +t.getId()+"++"+ t.getId());
        System.out.println(UserRuntime.getUser());
        if (gardenPlotService.saveOrUpdate(gardenPlot)){
            return R.success("success");
        }else{
            return R.fail("error");
        }
    }

    @RequestMapping("/delete")
    public R<String> delete(@RequestParam("id") String id){
        if (gardenPlotService.deltePlot(id)){
            return R.success("success");
        }else{
            return R.fail("error");
        }
    }

    @RequestMapping("/getList")
    public R<List<GardenPlot>> getList(@RequestBody GardenPlot gardenPlot){
        return R.success(gardenPlotService.getList(gardenPlot));
    }

    @RequestMapping("/export")
    public void export(@RequestBody GardenPlot gardenPlot, HttpServletResponse response){

        String fileName = "园地信息统计.xls";
        List<GardenPlot> list = new ArrayList<>();
        if (gardenPlot.getExportType() == 1) {
            fileName = "园地信息统计模板.xls";
            list.add(new GardenPlot());
        }else {
            list = gardenPlotService.getList(gardenPlot);
        }
        try(final OutputStream os = response.getOutputStream()){
            response.reset();
            response.setContentType("application/csv;charset=UTF-8");
            response.setHeader("Pragma", "public");
            response.setHeader("Content-Disposition", "attachment; filename="+ URLEncoder.encode( fileName, "UTF-8"));
            response.setCharacterEncoding("UTF-8");
            Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), GardenPlot.class, list);
            workbook.write(os);
            os.flush();
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/import")
    public R<String> importList(@RequestParam("file") MultipartFile file){
        if (file.isEmpty()){
            return R.fail("文件为空");
        }
        try {
            List<GardenPlot> gardenPlotList = ExcelImportUtil.importExcel(file.getInputStream(), GardenPlot.class, new ImportParams());
            for (GardenPlot gardenPlot : gardenPlotList){
                saveOrUpdate(gardenPlot);

            }
            return R.success("导入成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.fail("导入失败");
    }
}
